<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="10">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            @keydown.native.enter="getGoodsList"
            @clear="getGoodsList"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 数据表格 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款">
          <template v-slot="scope">
            <el-tag
              size="mini"
              v-if="scope.row.order_pay === '1'"
              type="success"
              >已付款</el-tag
            >
            <el-tag size="mini" v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template v-slot="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="showBox"
            ></el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-location"
              @click="showProgressBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址的对话框 -->
    <el-dialog title="修改地址" :visible.sync="aaddressVisible" width="50%">
      <!-- 内容 -->
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityDate"
            :props="props"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="aaddressVisible = false">取 消</el-button>
        <el-button type="primary" @click="aaddressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 展示物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressVisible"
      width="50%"
    >
      <!-- 内容 -->
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityDate from './citydata'
export default {
  data() {
    return {
      // timeline数据
      reverse: true,
      activities: [
        {
          content: '快递已签收 签收人:马保国 感谢使用飞兔快递，期待再次为您服务',
          timestamp: '2018-04-15'
        },
        {
          content: '快递到达自取点，请取件',
          timestamp: '2018-04-13'
        },
        {
          content: '快递已揽件 揽件人：张啊满',
          timestamp: '2018-04-11'
        }
      ],
      // 查询表单
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 订单数据
      orderList: [],
      total: 0,
      // 控制修改地址对话框显示隐藏
      aaddressVisible: false,
      // 物流进度
      progressVisible: false,
      // 修改地址表单数据
      addressForm: {
        address1: [],
        address2: ''
      },
      // 地址表单的验证规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 城市对象
      cityDate: cityDate,
      // 级联配置对象
      props: {
        expandTrigger: 'hover'
      }
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      console.log(res)
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 搜索
    getGoodsList() {
      this.getOrderList()
      console.log(this.queryInfo.query)
    },
    // 条数变化
    handleSizeChange(newSize) {
      // 更新条数
      this.queryInfo.pagesize = newSize
      // 请求
      this.getOrderList()
    },
    // 页码变化
    handleCurrentChange(newPage) {
      // 更新页码
      this.queryInfo.pagenum = newPage
      // 请求
      this.getOrderList()
    },
    // 修改地址对话框
    showBox() {
      this.aaddressVisible = true
    },
    // 关闭对话框重置表单
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 监听物流对话框
    async showProgressBox() {
      // 获取物流数据
      // const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      // if (res.meta.status !== 200) return this.$message.error('获取物流信息失败')

      this.progressVisible = true
    }
  }
}
</script>

<style lang="less" scoped></style>
